package deep.projects.newsapp.activities;

import android.os.Bundle;
import android.widget.EditText;

import com.wuxinwudai.android.util.SecurityUtils;
import com.wuxinwudai.android.util.StringUtils;
import com.wuxinwudai.android.util.ToastUtils;
import com.wuxinwudai.android.util.ViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import deep.projects.newsapp.R;
import deep.projects.newsapp.app.Urls;
import im.yixin.sdk.util.StringUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author 吾心无待 于 2016年07月18日
 */
public class ResetPasswordActivity extends BaseLayoutActivity{
    @BindView(R.id.et_smscode) EditText metCode;
    @BindView(R.id.et_confirm_password) EditText metPassword;
    private String mMobile;
    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        tvTitle.setText("密码重置");
        mMobile = getIntent().getStringExtra("mobile");
    }

    @OnClick(R.id.btn_login) void login(){
        String code = metCode.getText().toString();
        String pwd = metPassword.getText().toString();
        if (StringUtils.isNullOrWhiteSpace(pwd)){
            ToastUtils.showShort(this,"请输入6-16位密码！");
            return;
        }
        ViewUtils.showLoadingDialog(this);
        OkHttpUtils.post().url(Urls.POST_LOGIN)
                .addParams("mobile",mMobile)
                .addParams("code",code)
                .addParams("password", SecurityUtils.md5(pwd)).build().execute(new Callback<JSONObject>() {
            @Override
            public JSONObject parseNetworkResponse(Response response) throws Exception {
                return new JSONObject(response.body().string());
            }
            @Override
            public void onError(Call call, Exception e) {
                i("on Error:"+e.getMessage());
                ViewUtils.dismissLoadingDialog();
            }
            @Override
            public void onResponse(final JSONObject response) {
                try{
                    if (response.getBoolean("ok")){
                        //登录
                    }else{
                        ToastUtils.showShort(ResetPasswordActivity.this,response.getString("message"));
                    }
                }catch (Exception ex){

                }
                ViewUtils.dismissLoadingDialog();
            }
        });
    }
}
